package com.yvtian.aspect;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.yvtian.admin.vo.Permission;
import com.yvtian.admin.vo.UserInfoResponse;
import com.yvtian.annotation.PreAuthorize;
import com.yvtian.constants.TokenConstants;
import com.yvtian.exception.PreAuthorizeException;
import com.yvtian.utils.RedisConstant;
import com.yvtian.utils.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Aspect
@Component
@Order(value = 10)
public class PreAuthorizeAspect {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Pointcut(value = "@annotation(com.yvtian.annotation.PreAuthorize)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before() {
    }

    @After("pointCut()")
    public void after() {
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void after(JoinPoint joinPoint, Object result) {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        PreAuthorize annotation = methodSignature.getMethod().getAnnotation(PreAuthorize.class);
//        String methodName = methodSignature.getMethod().getName();
//        if (null == annotation) return joinPoint.proceed();
//        if (!StringHelper.isEmpty(annotation.hasPermission())) {
//            if (!hasPermission(annotation.hasPermission())) {
//                log.info("methodName->{}", methodName);
//                log.info("permissionName->{}", annotation.hasPermission());
//                throw new PreAuthorizeException("无权限访问");
//            }
//        }

        return joinPoint.proceed();
    }


    public boolean hasPermission(String per)   {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        String token = request.getHeader(TokenConstants.TOKEN_VALUE);
        String userId = (String) request.getAttribute(TokenConstants.JWT_USER_ID);

//        Claims claims = JwtTokenUtil.getInfoFromToken(token, TokenConstants.SECRET_KEY);
//        JWTInfo jwtInfo = new JWTUtil().getJWTInfo(claims);
        String s = redisTemplate.opsForValue().get(RedisConstant.REDIS_USER_INFO_KEY + ":" + userId);
        try {
            UserInfoResponse userInfoResponse = new Gson().fromJson(s, UserInfoResponse.class);
            for (Permission permission : userInfoResponse.getPermissions()) {
                if (per.equals(permission.getPerCode()) || permission.getIsDisable() == 0) {
                    return true;
                }
            }
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return false;
    }


}
